package controllers

import java.io.File

import config.{Global, MyRow}
import dao.RefDao
import javax.inject.Inject
import play.api.libs.json.Json
import play.api.mvc.{AbstractController, ControllerComponents}

import scala.concurrent.ExecutionContext

class RefController @Inject()(refDao: RefDao, cc: ControllerComponents)
                             (implicit exec: ExecutionContext) extends AbstractController(cc) with MyRow {

  def refPage = Action{
    Ok(views.html.ref.refPage())
  }

  def getAllRefInfo = Action.async { implicit request =>
    refDao.getAll.map { x =>
      Ok(Json.toJson(x.map(_.getJsonByT())))
    }
  }

  def openRefPdf(id:String,name: String) = Action { implicit request =>
    val filename = new String(("filename=\"" + s"${Global.path}/ref/$name.pdf" + "\"").getBytes("GBK"), "ISO-8859-1")
    Ok.sendFile(new File(Global.path + "/ref/" + id + ".pdf")).withHeaders(
      //缓存
      CACHE_CONTROL -> "max-age=3600",
      CONTENT_DISPOSITION -> filename,
      CONTENT_TYPE -> "text/plain"
    )
  }

}
